﻿using System.Globalization;
using System.Linq;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace EncryptDecrypt
{
    internal class SymmetricKeyDataServiceContext : TableServiceContext
    {
        public SymmetricKeyDataServiceContext(CloudStorageAccount acct)
        : base(acct.TableEndpoint.AbsoluteUri, acct.Credentials)
        {
        }

        public SymmetricKeyDataServiceContext(string baseAddress, StorageCredentials credentials)
        : base(baseAddress, credentials)
        {
        }

        public const string TableName = "SymmetricKeys";

        public IQueryable<SymmetricKey> SymmetricKeys
        {
            get
            {
                return this.CreateQuery<SymmetricKey>(TableName);
            }
        }

        public void SaveSymmetricKey(SymmetricKey sKey)
        {
            sKey.RowKey = sKey.Version.ToString(CultureInfo.InvariantCulture);
            
            this.AddObject(TableName, sKey);
            this.SaveChangesWithRetries();
        }
    }
}